#ifndef SROS_DEVICE_DETAIL_MOTEC_HPP
#define SROS_DEVICE_DETAIL_MOTEC_HPP

#include <commons/type/ret.hpp>

#include "driver_impl.hpp"

#include <memory>
#include <atomic>

namespace sros::async
{
    class Timer;
}

namespace sros::device
{
    class McuDevice;
}

namespace sros::device::detail
{
    class MotecDriver final : public driver_impl
    {
    public:
        MotecDriver() = default;
        MotecDriver(const Param &param, std::shared_ptr<McuDevice> mcu); 
        ~MotecDriver() = default;

        // 初始化
        void init() override;

        // 运行
        void run() override;

        // 关闭
        void close() override;

        // 解析反馈报文
        void parse(const device::proto::RspCanMsg &can_msg) override;

        // 故障清除
        RetBool clear_error() override;

        // 设置转速 mm/s
        RetBool set_spd(const int32_t &spd) override;

        // 设置电流 mA/s
        RetBool set_current(const int32_t &current) override;

        // 上下使能
        RetBool set_enable(const bool &enable) override;

        // 故障检测
        void error_check() override;

        RetBool set_control(const uint16_t &control_word);

    private:
        void on_timer_trigger_(std::size_t times);
        void on_req_timer_trigger_(std::size_t times);

        RetBool send_can_msg_(hal::CanTxMsg &tx_msg);

    private:
        std::atomic<uint64_t>         last_can_recv_timer_;
        std::atomic<bool>             last_conn_state_;
        std::atomic<uint32_t>         detect_stall_times_;
        std::shared_ptr<McuDevice>    mcu_;
        std::shared_ptr<async::Timer> timer_;
        std::shared_ptr<async::Timer> req_timer_;
    };
}


#endif